package api

import (
	"encoding/json"
	"github.com/mssola/user_agent"
	"github.com/oschwald/geoip2-golang"
	"go.uber.org/zap"
	"jms/global"
	"jms/models"
	"net"
	"time"
)

func PostTimePage(string2 string, Ip string, UA string) {

	var FaChannelTimePage models.FaChannelTimePage
	err := json.Unmarshal([]byte(string2), &FaChannelTimePage)
	if err != nil {
		zap.S().Info("[PostTimePage]参数转吗失败", err)
	}
	FaChannelTimePage.CreatedTime = time.Now()
	FaChannelTimePage.UA = UA
	FaChannelTimePage.IP = Ip
	if FaChannelTimePage.Channel == "" {
		return
	}

	geoDB, err := geoip2.Open("./GeoLite2-City.mmdb")
	record, err := geoDB.City(net.IP(Ip))
	if err != nil {
		zap.S().Info("查询城市信息失败", err)
	}
	FaChannelTimePage.Address = record.City.Names["zh-CN"] // 获取中文城市名
	if FaChannelTimePage.Address == "" {
		FaChannelTimePage.Address = record.City.Names["en"] // 如果没有中文城市名，则获取英文城市名
	}
	//// 解析ua
	uaParser := user_agent.New(FaChannelTimePage.UA)
	// 提取机型信息
	FaChannelTimePage.Model = uaParser.Model()

	tx := global.Db.Create(&FaChannelTimePage)
	if tx.Error != nil {
		zap.S().Info("PostTimePage error ", tx.Error)
	}
}
